home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 4 / The 640 Meg Shareware Studio CD-ROM Volume IV (Data Express)(1994).ISO / clang / 3d_lib.zip / AF.C < prev    next >
C/C++ Source or Header  |  1993-05-09  |  6KB  |  167 lines

  1. /* Add face to object
  2.  
  3.    Copyright (c) 1988 by Gus O'Donnell
  4.  
  5.    Revision history:
  6.  
  7.    Version 1.00         February 29, 1988       As released.
  8.  
  9.    Version 1.01         March 20, 1988          Created libraries for all
  10.                                                 memory models
  11.  
  12. */
  13. #include "3d.h"
  14. #include <alloc.h>
  15. #include <float.h>
  16. #include <math.h>
  17. #include <stdio.h>
  18.  
  19. int     add_face (OBJECT *this_obj, FACE *this_face)
  20.  
  21. /* Add a face to an object.  The initial data structure looks like this:
  22.  
  23.  
  24.                OBJECT o o------------------------------+
  25.                       |                                |
  26.                       V                                V
  27.                      FACE o X                        VERTEX o
  28.                           |                                 |
  29.                       +---+                            +----+
  30.                       |                                |
  31.                       V                                V
  32.                      FACE X X                        VERTEX X
  33.  
  34.  
  35. Where 'X' is the NULL pointer.  The revised structure looks like this:
  36.  
  37.  
  38.                OBJECT o o------------------------------+
  39.                       |                                |
  40.                       V                                V
  41.                      FACE o X   +-->CORNER o X       VERTEX o
  42.                           |     |          |                |
  43.                       +---+     |     +----+           +----+
  44.                       |         |     |                |
  45.                       V         |     V                V
  46.                      FACE o o---+   CORNER o o------>VERTEX o
  47.                           |                |                |
  48.                       +---+           +----+           +----+
  49.                       |               |                |
  50.                       V               V                V
  51.                      FACE X X       CORNER o o------>VERTEX o
  52.                                            |                |
  53.                                       +----+           +----+
  54.                                       |                |
  55.                                       V                V
  56.                                     CORNER o o------>VERTEX o
  57.                                            |                |
  58.                                       +----+           +----+
  59.                                       |                |
  60.                                       V                V
  61.                                     CORNER X X       VERTEX X
  62.  
  63.  
  64. The corners of the face are merged into the vertex list of the object,
  65. sorted by maximum z, then maximum y, then maximum x.  If a duplicate
  66. vertex is encountered, the corner is set to point at the existing vertex,
  67. and the duplicate is discarded.
  68.  
  69. Return status as follows:
  70.  
  71.                  0 - success,
  72.                  1 - face has less than 3 corners.
  73. */
  74.  
  75. {
  76.     typedef enum {insert,replace,next} ACTION;
  77.  
  78.     int count;
  79.     CORNER *chandle;
  80.     VERTEX *vhandle,*v1,*v2;
  81.     ACTION action;
  82.  
  83.     /* Check number of corners */
  84.  
  85.     chandle = this_face -> first -> next;
  86.     for (count = 1;count <= 3;count++)
  87.     {
  88.         if (chandle -> next == NULL) return (1);
  89.         chandle = chandle -> next;
  90.     }
  91.  
  92.     /* Link in the face at the beginning of the list */
  93.  
  94.     this_face -> next = this_obj -> faces -> next;
  95.     this_obj -> faces -> next = this_face;
  96.  
  97.     chandle = this_face -> first;
  98.     while (chandle -> next -> next != NULL)
  99.     {
  100.         vhandle = this_obj -> vertices;
  101.         action = next;
  102.         while (action == next)   /* Search the vertex list.  Pseudodata in */
  103.         {                        /* tail node prevents running off end */
  104.  
  105.         /* The action taken depends on the following conditions:
  106.  
  107.                z1 ? z2   y1 ? y2   x1 ? x2    Action
  108.              +---------+---------+---------+----------+
  109.              |    <    |    X    |    X    |  Insert  |
  110.              |    =    |    <    |    X    |  Insert  |
  111.              |    =    |    =    |    <    |  Insert  |
  112.              |    =    |    =    |    =    |  Replace |
  113.              |    =    |    =    |    >    |  Next    |
  114.              |    =    |    >    |    X    |  Next    |
  115.              |    >    |    X    |    X    |  Next    |
  116.              +---------+---------+---------+----------+
  117.  
  118.         Where [x1 y1 z1] is the vertex pointed to by chandle -> next -> this,
  119.           and [x2 y2 z2] is the vertex pointed to by vhandle -> next.
  120.         */
  121.  
  122.             v1 = chandle -> next -> this;
  123.             v2 = vhandle -> next;
  124.             if (v1 -> coord [2] < v2 -> coord [2])
  125.                 action = insert;
  126.             else
  127.             {
  128.                 if (v1 -> coord [2] == v2 -> coord [2])
  129.                 {
  130.                     if (v1 -> coord [1] < v2 -> coord [1])
  131.                         action = insert;
  132.                     else
  133.                     {
  134.                         if (v1 -> coord [1] == v2 -> coord [1])
  135.                         {
  136.                             if (v1 -> coord [0] < v2 -> coord [0])
  137.                                 action = insert;
  138.                             else
  139.                             {
  140.                                 if (v1 -> coord [0] == v2 -> coord [0])
  141.                                     action = replace;
  142.                                 else
  143.                                     action = next;
  144.                             }
  145.                         }
  146.                     }
  147.                 }
  148.             }
  149.             switch (action)
  150.             {
  151.                 case insert:
  152.                     v1 -> next = v2;
  153.                     vhandle -> next = v1;
  154.                     break;
  155.                 case replace:
  156.                     chandle -> next -> this = v2;
  157.                     free (v1);
  158.                     break;
  159.                 case next:
  160.                     vhandle = vhandle -> next;
  161.             }
  162.         }
  163.         chandle = chandle -> next;
  164.     }
  165.     return(0);
  166. }
  167.